#pragma once

namespace SystemsOptimizer 
{
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Drawing;
	using namespace System::IO;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  bttnLoadFile;
	protected: 
	private: System::Windows::Forms::OpenFileDialog^  openFileDialog;
	private: System::Windows::Forms::ListBox^  listBox;


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->bttnLoadFile = (gcnew System::Windows::Forms::Button());
			this->openFileDialog = (gcnew System::Windows::Forms::OpenFileDialog());
			this->listBox = (gcnew System::Windows::Forms::ListBox());
			this->SuspendLayout();
			// 
			// bttnLoadFile
			// 
			this->bttnLoadFile->Location = System::Drawing::Point(13, 13);
			this->bttnLoadFile->Name = L"bttnLoadFile";
			this->bttnLoadFile->Size = System::Drawing::Size(75, 23);
			this->bttnLoadFile->TabIndex = 0;
			this->bttnLoadFile->Text = L"Load File";
			this->bttnLoadFile->UseVisualStyleBackColor = true;
			this->bttnLoadFile->Click += gcnew System::EventHandler(this, &Form1::bttnLoadFile_Click);
			// 
			// openFileDialog
			// 
			this->openFileDialog->Filter = L"Text files (*.txt)|*.txt";
			this->openFileDialog->Multiselect = true;
			// 
			// listBox
			// 
			this->listBox->FormattingEnabled = true;
			this->listBox->Location = System::Drawing::Point(13, 43);
			this->listBox->Name = L"listBox";
			this->listBox->Size = System::Drawing::Size(419, 212);
			this->listBox->TabIndex = 1;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(444, 262);
			this->Controls->Add(this->listBox);
			this->Controls->Add(this->bttnLoadFile);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
			this->MinimizeBox = false;
			this->Name = L"Form1";
			this->ShowIcon = false;
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"Systems Optimizer";
			this->ResumeLayout(false);

		}
#pragma endregion

	private: 
		System::Void bttnLoadFile_Click(System::Object^  sender, System::EventArgs^  e) 
		{
			if(openFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK)
			{
				InputFileHandler file;
				string aux = msclr::interop::marshal_as<string>(Path::GetFileNameWithoutExtension( openFileDialog->FileName ));
				Stock st(aux);
				
				file.ReadQuotations( msclr::interop::marshal_as<string>(openFileDialog->FileName), &st);

				vector<Quotation> stockQuots = st.GetQuotations();

				vector<Indicator*> ind;
				ind.push_back(new SMA(CLOSE, 25));
				ind.push_back(new EMA(CLOSE, 9));

				for(int j=0; j<(int)ind.size(); j++)
					ind[j]->Initialize(stockQuots);

				TradeSystem *system = new MovAvgCrossOver(ind[0]->GetValues(), ind[1]->GetValues(), .0, .0);
				BackTester bt;

				bt.StartTest(stockQuots, 0, stockQuots.size(), system);
				BTStatistics stats = bt.GetStatistics();

				listBox->Items->Add(String::Format( "Num Trades: {0}", stats.GetNumTrades() ));
				listBox->Items->Add(String::Format( "Num Wins: {0}", stats.GetNumWins() ));
				listBox->Items->Add(String::Format( "Net Profit: {0}", stats.GetNetProfit() ));

				vector<Trade> buySellTrades = stats.GetBuySellTrades();
				vector<Trade> shortCoverTrades = stats.GetShortCoverTrades();

				for(int i=0; i< buySellTrades.size(); i++){
					if(buySellTrades[i].Type == _BUY) {
						listBox->Items->Add(String::Format("BUY\t{0}/{1}/{2} {3}:{4}:{5}\t{6}", buySellTrades[i].Dt.Date.Day,
																								buySellTrades[i].Dt.Date.Month,
																								buySellTrades[i].Dt.Date.Year,
																								buySellTrades[i].Dt.Date.Hour,
																								buySellTrades[i].Dt.Date.Minute,
																								buySellTrades[i].Dt.Date.Second,
																								buySellTrades[i].Price));
					}
					else {
						listBox->Items->Add(String::Format("SELL\t{0}/{1}/{2} {3}:{4}:{5}\t{6}", buySellTrades[i].Dt.Date.Day,
																								buySellTrades[i].Dt.Date.Month,
																								buySellTrades[i].Dt.Date.Year,
																								buySellTrades[i].Dt.Date.Hour,
																								buySellTrades[i].Dt.Date.Minute,
																								buySellTrades[i].Dt.Date.Second,
																								buySellTrades[i].Price));
					}
				}

				for(int i=0; i< shortCoverTrades.size(); i++){
					if(shortCoverTrades[i].Type == _SHORT) {
						listBox->Items->Add(String::Format("SHORT\t{0}/{1}/{2} {3}:{4}:{5}\t{6}", shortCoverTrades[i].Dt.Date.Day,
																								shortCoverTrades[i].Dt.Date.Month,
																								shortCoverTrades[i].Dt.Date.Year,
																								shortCoverTrades[i].Dt.Date.Hour,
																								shortCoverTrades[i].Dt.Date.Minute,
																								shortCoverTrades[i].Dt.Date.Second,
																								shortCoverTrades[i].Price));
					}
					else {
						listBox->Items->Add(String::Format("COVER\t{0}/{1}/{2} {3}:{4}:{5}\t{6}", shortCoverTrades[i].Dt.Date.Day,
																								shortCoverTrades[i].Dt.Date.Month,
																								shortCoverTrades[i].Dt.Date.Year,
																								shortCoverTrades[i].Dt.Date.Hour,
																								shortCoverTrades[i].Dt.Date.Minute,
																								shortCoverTrades[i].Dt.Date.Second,
																								shortCoverTrades[i].Price));
					}
				}
			}
		}
	};
}

